// pages/test/test.js
Page({
  data: {
    currentQuestion: null,
    currentIndex: 0,
    questions: [],
    selectedOption: -1,
    isAnswerCorrect: null,
    score: 0
  },

  // 选择选项
  selectOption(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      selectedOption: index
    })
  },

  // 提交答案
  submitAnswer() {
    const { currentQuestion, selectedOption, questions, currentIndex, score } = this.data

    if (selectedOption === -1) {
      wx.showToast({
        title: '请选择一个答案',
        icon: 'none'
      })
      return
    }

    // 检查答案是否正确
    const isCorrect = selectedOption === currentQuestion.correctAnswer
    const newScore = isCorrect ? score + 10 : score

    this.setData({
      isAnswerCorrect: isCorrect,
      score: newScore
    })

    // 延迟显示下一题
    setTimeout(() => {
      this.nextQuestion()
    }, 1500)
  },

  // 下一题
  nextQuestion() {
    let { currentIndex, questions } = this.data
    currentIndex++

    if (currentIndex >= questions.length) {
      // 测试完成
      wx.showModal({
        title: '测试完成',
        content: `您的得分是: ${this.data.score}/${questions.length * 10}`,
        showCancel: false,
        success: () => {
          wx.navigateBack()
        }
      })
      return
    }

    this.setData({
      currentIndex,
      currentQuestion: questions[currentIndex],
      selectedOption: -1,
      isAnswerCorrect: null
    })
  },

  onLoad: function () {
    // 模拟测试题数据
    const questions = [
      {
        id: 1,
        word: 'confidential',
        options: ['公开的', '自信的', '机密的，保密的', '困惑的'],
        correctAnswer: 2
      },
      {
        id: 2,
        word: 'elaborate',
        options: ['简单的', '详尽的', '快速的', '困难的'],
        correctAnswer: 1
      },
      {
        id: 3,
        word: 'ambition',
        options: ['野心，雄心', '恐惧', '懒惰', '善良'],
        correctAnswer: 0
      }
    ]

    this.setData({
      questions,
      currentQuestion: questions[0]
    })
  }
})